package com.dingpeikeji.quickdeer.modules.rentproduct.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.modules.bus.entity.TCompBus;
import com.dingpeikeji.quickdeer.core.modules.schedef.entity.TSchedefVehi;
import com.dingpeikeji.quickdeer.core.modules.sys.dao.OfficeDao;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TRentgroupDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TRentprodPriceDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TRentgroupAuditDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TRentproductDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroup;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentprodPrice;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroupAudit;

/**
 * 发布整车Service
 * 
 * @author wuhao
 * @version 2016-08-08
 */
@Service
@Transactional(readOnly = true)
public class TRentgroupService extends CrudService<TRentgroupDao, TRentgroup> {
	@Autowired
	private OfficeDao officeDao;
	@Autowired
	private TRentgroupAuditDao rentgroupAuditDao;
	@Autowired
	private TRentproductDao rentproductDao;
	@Autowired
	private TRentgroupDao tRentgroupDao;

	public TRentgroup get(Integer rentgrpid) {
		TRentgroup tRentgroup = dao.get(rentgrpid);
		return tRentgroup;
	}

	public List<TRentgroup> findList(TRentgroup TRentgroup) {
		if (TRentgroup.getOffice() == null) {
			User user = UserUtils.getUser();
			// 设置部门
			Office office = user.getOffice();
			TRentgroup.setOffice(office);
		}
		return super.findList(TRentgroup);
	}

	public Page<TRentgroup> findPage(Page<TRentgroup> page, TRentgroup TRentgroup) {
		if (TRentgroup.getOffice() == null) {
			User user = UserUtils.getUser();
			// 设置部门
			Office office = user.getOffice();
			TRentgroup.setOffice(office);
		}
		return super.findPage(page, TRentgroup);
	}
	
	public Page<TRentgroup> findAll(Page<TRentgroup> page, TRentgroup TRentgroup) {
		return super.findPage(page, TRentgroup);
	}

	@Transactional(readOnly = false)
	public void save(TRentgroup tRentgroup) {
		if (tRentgroup.getRentgrpid() == null) {
			super.save(tRentgroup);
		} else {
			dao.update(tRentgroup);
		}
		List<TRentproduct> tRentproductList = tRentgroup.getProductList();
		if (tRentproductList != null && !tRentproductList.isEmpty()) {
			for (TRentproduct tRentproduct : tRentgroup.getProductList()) {
				if (tRentproduct.getId() != null) {
					// 通过车牌号 查询当前车辆信息
					if (tRentproduct.getRentid() == null) {
						tRentproduct.setOffice(tRentgroup.getOffice());
						tRentproduct.setRentgrpid(tRentgroup.getRentgrpid());
						tRentproduct.setDepartcity(tRentgroup.getDepartcity());
						tRentproduct.setDestcity(tRentgroup.getDestcity());
						tRentproduct.setRentType(tRentgroup.getRenttype());
						if (tRentgroup.gettLine() != null)
							tRentproduct.setDaysnum(tRentgroup.gettLine().getEstdays());
						rentproductDao.insert(tRentproduct);
					} else {
						tRentproduct.setDepartcity(tRentgroup.getDepartcity());
						tRentproduct.setDestcity(tRentgroup.getDestcity());
						tRentproduct.setRentType(tRentgroup.getRenttype());
						if (tRentgroup.gettLine() != null)
							tRentproduct.setDaysnum(tRentgroup.gettLine().getEstdays());
						rentproductDao.update(tRentproduct);
					}
				} else {
					rentproductDao.delete(tRentproduct);
				}
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(TRentgroup TRentgroup) {
		super.delete(TRentgroup);
	}

	// 获取旅行商和电商列表
	public List<Office> findOfficeList() {
		Office office = new Office();

		// TODO 暂时通过两条查询语句获取旅行商和电商列表
		office.setFlatType(Office.FLAT_TYPE_ENTERPRISE);
		List<Office> list = officeDao.findList(office);
		office.setFlatType(Office.FLAT_TYPE_EBUSINESS);
		list.addAll(officeDao.findList(office));
		return list;
	}

	public List<TRentgroup> getTravelProducts(TRentgroup tRentgroup) {
		return tRentgroupDao.getTravelProducts(tRentgroup);
	}

	public List<Integer> getTravelProductDaysNum(TRentgroup tRentgroup) {
		return tRentgroupDao.getTravelProductDaysNum(tRentgroup);
	}

	public List<String> getTravelProductSiteList(TRentgroup tRentgroup) {
		return tRentgroupDao.getTravelProductSiteList(tRentgroup);
	}

	public String getDescriptionById(String linename) {
		return tRentgroupDao.getDescriptionById(linename);
	}

	public List<String> getTravelSiteList(TRentgroup tRentgroup) {
		return tRentgroupDao.getTravelSiteList(tRentgroup);
	}

	public List<TRentgroup> getMobileTravelProducts(TRentgroup tRentgroup) {
		return tRentgroupDao.getMobileTravelProducts(tRentgroup);
	}

	public TRentgroup getMobileRentgroup(Integer rentgrpid) {
		return tRentgroupDao.getMobileRentgroup(rentgrpid);
	}


}